﻿using DevExpress.XtraEditors.Controls;
using DevExpress.XtraReports.UI;
using EyFramework.Common;
using EyFramework.Common.Utils;
using EyFramework.Library;
using Newtonsoft.Json;
using ShuJuGj.Core.Service;
using ShuJuGj.Entity;
using ShuJuGj.Report;
using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using tjyyjl.Core.Service;

namespace ShuJuGj
{
    public partial class FrmTjTjyy : FrmDevBase
    {
        private List<GridColumnInfo> columnInfos = new List<GridColumnInfo>();
        private Dictionaries dictionaries = new Dictionaries();
        private ITjTjyyjlService tjTjyyjlService;
        private String token = "";
        private String Tx_token = "";
        private IniFileUtil iniFile = new IniFileUtil(Path.Combine(DirectoryUtil.GetCurrentDirectory(), "application.ini"));

        public FrmTjTjyy()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 查询按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnQuery_Click(object sender, EventArgs e)
        {
            ShowLoading();
            try
            {
                InitForm();
                QueryData();
            }
            finally
            {
                CloseLoading();
            }
        }

        

        /// <summary>
        /// 开启界面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmNcov_Load(object sender, EventArgs e)
        {
            
            tjTjyyjlService = Globals.GetService<ITjTjyyjlService>();
            TJ.Text = "预约时间";
            de_start.Text = DateTime.Now.ToString("yyyy-MM-dd");
            de_end.Text = DateTime.Now.ToString("yyyy-MM-dd");
            GetToken();
            GetToken_Tx();
            InitForm();
            QueryData();
        }

       

        private void InitForm()
        {
            tjTjyyjlService = Globals.GetService<ITjTjyyjlService>();
            gvData.ShowRowNum();
            gcData.ShowContextMenu(this.components);//显示右键导出功能
        }

        /// <summary>
        /// 读取数据库中的信息
        /// </summary>
        private void QueryData()
        {
            Inquire inquire = new Inquire
            {
                daojiansjStart= TJ.Text.Trim() == "到检时间" ? de_start.Text.Trim() : "",
                daojiansjEnd = TJ.Text.Trim() == "到检时间" ? de_end.Text.Trim() : "",
                checkDateStart = TJ.Text.Trim()=="预约时间" ? de_start.Text.Trim() : "",
                checkDateEnd = TJ.Text.Trim() == "预约时间" ? de_end.Text.Trim() : "",
                name = tePatnm.Text.Trim(),
                orderStatus = comboBoxEdit1.Text.Trim() == "已预约" ? "1" : comboBoxEdit1.Text.Trim() == "已接收" ? "2" : comboBoxEdit1.Text.Trim() == "到检" ? "3" : comboBoxEdit1.Text.Trim() == "取消预约" ? "-1" : "",
                laiyuan= comboBoxEdit2.Text.Trim(),
            };
            var tjyy = tjTjyyjlService.GetListBy(inquire);
            tjyy = dictionaries.Change(tjyy);
            gcData.DataSource = tjyy;
            DevPublic.SetBindGridControlView(gvData, true, true);
        }

        /// <summary>
        /// 接收与到检按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ribBtnEdit_ButtonClick(object sender, ButtonPressedEventArgs e)
        {
            if (e.Button.Caption.Equals("接收"))
            {
                Get();
            }
            else if (e.Button.Caption.Equals("到检"))
            {
                if (Globals.ShowYesNoAndTips("该病人是否到检？") == DialogResult.Yes)
                {
                    Set();
                }
            }
        }


        /// <summary>
        /// 信息同步按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void simpleButton1_Click(object sender, EventArgs e)
        {
            GetData();
            GetData_Tx();
            InitForm();
            QueryData();
        }

        /// <summary>
        /// 清除查询条件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void simpleButton2_Click(object sender, EventArgs e)
        {
            de_start.Text = DateTime.Now.ToString("yyyy-MM-dd");
            de_end.Text = DateTime.Now.ToString("yyyy-MM-dd");
            comboBoxEdit1.Text = "";
            tePatnm.Text = "";
            TJ.Text = "预约时间";
            InitForm();
            QueryData();
        }

        /// <summary>
        /// 打印按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void simpleButton3_Click(object sender, EventArgs e)
        {
            Inquire inquire = new Inquire
            {
                daojiansjStart = TJ.Text.Trim() == "到检时间" ? de_start.Text.Trim() : "",
                daojiansjEnd = TJ.Text.Trim() == "到检时间" ? de_end.Text.Trim() : "",
                checkDateStart = TJ.Text.Trim() == "预约时间" ? de_start.Text.Trim() : "",
                checkDateEnd = TJ.Text.Trim() == "预约时间" ? de_end.Text.Trim() : "",
                name = tePatnm.Text.Trim(),
                orderStatus = comboBoxEdit1.Text.Trim() == "已预约" ? "1" : comboBoxEdit1.Text.Trim() == "已接收" ? "2" : comboBoxEdit1.Text.Trim() == "到检" ? "3" : comboBoxEdit1.Text.Trim() == "取消预约" ? "-1" : ""
            };
            TjReport report = new TjReport();
            report.PrintData(inquire);
            report.ShowPrintMarginsWarning = false;
            ReportPrintTool printTool = new ReportPrintTool(report);
            //打印预览
            //printTool.ShowPreview();
            //打印机配置
            if (iniFile.IniReadValue("Interface-TJ", "PRINT_NAME", "").Equals(""))
            {
                printTool.Print();//默认打印机输出
            }
            else
            {
                printTool.PrinterSettings.PrinterName = iniFile.IniReadValue("Interface-TJ", "PRINT_NAME");
                printTool.Print();//指定打印机输出
            }
        }

        /// <summary>
        /// 到检数据写入
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Get()
        {
            TjTjyyjl tjTjyyjl = gvData.GetRow(gvData.FocusedRowHandle) as TjTjyyjl;
            var aa = tjTjyyjl.orderStatus;
            if (tjTjyyjl.orderStatus.Equals("到检"))
            {
                Globals.ShowWarning("该病人已到检！无法接收！");
            }
            else
            {
                tjTjyyjl.jieshousj = DateTime.Now;
                tjTjyyjl.jieshourenxm = Globals.CurrentUser.StaffName;
                tjTjyyjl.orderStatus = "2";
                tjTjyyjlService.updateState(tjTjyyjl);
            }
            InitForm();
            QueryData();
        }

        /// <summary>
        /// 获取json数据从浙里办,并填入数据库
        /// </summary>
        private void GetData()
        {
            var stream = JsonConvert.DeserializeObject<Tjsjjson>(tjTjyyjlService.GetData(token,""));
            List<TjTjyyjl> results = stream.result;
            if (stream.code == 200 && stream.result != null)
            {
                //传输成功
                var dateNow = DateTime.Now;
                results.ForEach(p =>
                {
                    p.createDt = dateNow;
                    p.laiyuan = "浙里办体检";
                });
                tjTjyyjlService.Insert(results);
                SetDataTS(results);
            }
            else if (stream.code != 200)
            {
                Globals.ShowError(stream.message + "!" + stream.code);
                //数据错误
            }
            else if (stream.result == null) {
                Globals.ShowError("浙里办没有传入的数据！");
            }
        }

        /// <summary>
        /// 获取json数据从浙里办,并填入数据库
        /// </summary>
        private void GetData_Tx()
        {
            var stream = JsonConvert.DeserializeObject<Tjsjjson>(tjTjyyjlService.GetData_TX(Tx_token,""));
            List<TjTjyyjl> results = stream.result;
            if (stream.code == 200 && stream.result != null)
            {
                //传输成功
                var dateNow = DateTime.Now;
                results.ForEach(p =>
                {
                    p.createDt = dateNow;
                    p.laiyuan = "退休体检";
                });
                tjTjyyjlService.Insert(results);
                SetDataTS_Tx(results);
            }
            else if (stream.code != 200)
            {
                Globals.ShowError(stream.message + "!" + stream.code);
                //数据错误
            }
            else if (stream.result == null)
            {
                Globals.ShowError("退休没有传入的数据！");
            }
        }

        /// <summary>
        /// 收到数据条数
        /// </summary>
        /// <param name="results"></param>
        private void SetDataTS(List<TjTjyyjl> results)
        {
            //单条记录传：平台预约ID；多条记录传：项目编号
            if (results.Count > 1)
            {
                tjTjyyjlService.SetData(token,results[0].requestId, "");
            }
            else if (results.Count == 1)
            {
                tjTjyyjlService.SetData(token,"", results[0].serialNumber);
            }
            else
            {
                Globals.ShowError("浙里办没有传入的数据！");
            }
        }

        /// <summary>
        /// 收到数据条数
        /// </summary>
        /// <param name="results"></param>
        private void SetDataTS_Tx(List<TjTjyyjl> results)
        {
            //单条记录传：平台预约ID；多条记录传：项目编号
            if (results.Count > 1)
            {
                tjTjyyjlService.SetData_TX(Tx_token,results[0].requestId, "");
            }
            else if (results.Count == 1)
            {
                tjTjyyjlService.SetData_TX(Tx_token,"", results[0].serialNumber);
            }
            else
            {
                Globals.ShowError("退休系统没有传入的数据！");
            }
        }

        /// <summary>
        /// 获取浙里办token的值
        /// </summary>
        private void GetToken()
        {
            var stream = JsonConvert.DeserializeObject<Vucher>(tjTjyyjlService.GetToken());
            if (stream.code == 200)
            {
                //获取token值
                token = stream.result[0].token;
            }
            else
            {
                Globals.ShowError(stream.message + "!" + stream.code);
                //数据错误
            }
        }

        /// <summary>
        /// 获取退休体检token的值
        /// </summary>
        private void GetToken_Tx()
        {
           
            var stream = JsonConvert.DeserializeObject<Vucher>(tjTjyyjlService.GetToken_TX());
            if (stream.code == 200)
            {
                //获取token值
                Tx_token = stream.result[0].token;
            }
            else
            {
                Globals.ShowError(stream.message + "!" + stream.code);
                //数据错误
            }
        }

        /// <summary>
        /// 到检：数据修改
        /// </summary>
        private void Set()
        {
            TjTjyyjl tjTjyyjl = gvData.GetRow(gvData.FocusedRowHandle) as TjTjyyjl;
            if (tjTjyyjl.laiyuan == "退休体检")
            {
                if (tjTjyyjlService.Complete_TX(Tx_token,tjTjyyjl.serialNumber))
                {
                    tjTjyyjl.orderStatus = "3";
                    tjTjyyjl.daojiansj = DateTime.Now;
                    tjTjyyjlService.updateState(tjTjyyjl);
                }
            }
            else {
                if (tjTjyyjlService.Complete(token,tjTjyyjl.serialNumber))
                {
                    tjTjyyjl.orderStatus = "3";
                    tjTjyyjl.daojiansj = DateTime.Now;
                    tjTjyyjlService.updateState(tjTjyyjl);
                }
            }
            
            InitForm();
            QueryData();
        }


        
    }
}